﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ChanExt;
using System.ComponentModel;

namespace C.Unite
{
    /// <summary>
    /// 枚举处理类
    /// </summary>
    public class EnumHelper
    {


        #region##获得Enum类型description
        ///<summary>
        /// 获得Enum类型description
        /// 创建人：AnsonChan
        /// 创建时间：2013-1-20
        ///</summary>
        ///<param name="enumType">枚举的类型</param>
        ///<param name="val">枚举值</param>
        ///<returns>string</returns>
        public static string GetEnumDesc(Type enumType, object val)
        {
            string enumvalue = System.Enum.GetName(enumType, val);
            if (string.IsNullOrEmpty(enumvalue))
            {
                return "";
            }
            System.Reflection.FieldInfo finfo = enumType.GetField(enumvalue);
            object[] enumAttr = finfo.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true);
            if (enumAttr.Length > 0)
            {
                System.ComponentModel.DescriptionAttribute desc = enumAttr[0] as System.ComponentModel.DescriptionAttribute;
                if (desc != null)
                {
                    return desc.Description;
                }
            }
            return enumvalue;
        }
        #endregion


        public static int GetEnumValue(string n)
        {
            return 0;
        }

        #region##获取某个枚举的全部信息
        ///<summary>
        /// 获取某个枚举的全部信息
        /// 创建人：AnsonChan
        /// 创建时间：2013-1-20
        ///</summary>
        ///<typeparam name="T">枚举</typeparam>
        ///<returns>枚举的全部信息</returns>
        public static List<ReadEnum> GetEnumList<T>()
        {
            List<ReadEnum> list = new List<ReadEnum>();
            ReadEnum re = null;
            Type type = typeof(T);
            foreach (int enu in System.Enum.GetValues(typeof(T)))
            {
                re = new ReadEnum();
                re.Name = GetEnumDesc(type, enu);
                re.Value = enu;
                if (enu >= 0)
                    list.Add(re);
            }
            return list;
        }
        #endregion

        #region##根据值返回枚举对应的内容
        ///<summary>
        /// 根据值返回枚举对应的内容
        /// 创建人：AnsonChan
        /// 创建时间：2013-1-20
        ///</summary>
        ///<typeparam name="T">枚举</typeparam>
        ///<param name="value">值(int)</param>
        ///<returns></returns>
        public static T GetModel<T>(int value)
        {
            T myEnum = (T)System.Enum.Parse(typeof(T), value.ToString(), true);

            return myEnum;
        }
        #endregion

        #region##根据值返回枚举对应的内容
        ///<summary>
        /// 根据值返回枚举对应的内容
        /// 创建人：Porschev
        /// 创建时间：2013-1-20
        ///</summary>
        ///<typeparam name="T">枚举</typeparam>
        ///<param name="value">值(string)</param>
        ///<returns></returns>
        public static T GetModel<T>(string value)
        {
            T myEnum = (T)System.Enum.Parse(typeof(T), value, true);
            return myEnum;
        }
        #endregion
        #region ##绑定事件
        /// <summary>
        /// 下拉固定值绑定
        /// </summary>
        /// <typeparam name="T">枚举</typeparam>
        /// <param name="ddl">绑定的下拉ID</param>
        /// <returns></returns>
        public static DropDownList DrpBindDD<T>(DropDownList ddl)
        {
            List<ReadEnum> list = EnumHelper.GetEnumList<T>();
            ddl.DataSource = list;
            ddl.DataTextField = "Name";
            ddl.DataValueField = "Value";
            ddl.DataBind();
            return ddl;
        }
        #endregion
    }
    
    #region ##枚举实体
    public class ReadEnum
    {
        public string Name { get; set; }

        public int Value { get; set; }
    }
    #endregion
}